package com.zenika.zenbinding.client.validator;

import com.zenika.zenbinding.client.exception.DataMessage;
import com.zenika.zenbinding.client.exception.ValidatorException;

public class UrlValidator implements Validator {

	public UrlValidator() {
	}

	public void validate(Object object) throws ValidatorException {
		if (object != null && !"".equals(object.toString())) {

			boolean isMatching = object
					.toString()
					.matches(
							"((http|ftp|https):\\/\\/)?[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?");

			if (!isMatching) {
				DataMessage message = new DataMessage("URL is not valid");
				throw new ValidatorException(message);
			}
		}
	}
}
